/*
 *  Openmysee
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */
#include "stdafx.h"
#include ".\multilanguagemgr.h"

MultiLanguage::MultiLanguage() : m_innerbuflength(0), m_pinnerbuf(NULL)
{
	pCore = MultiLanguageCore::GetCore();
}

MultiLanguage::~MultiLanguage()
{
	delete[] m_pinnerbuf;
}

BOOL	MultiLanguage::isDllOK()
{
	return pCore != NULL;
}

MultiLanguage::MultiLanguageCore MultiLanguage::MultiLanguageCore::Core;

MultiLanguage::MultiLanguageCore::MultiLanguageCore()
{
	h_LanguageDLL = NULL;
}

MultiLanguage::MultiLanguageCore::~MultiLanguageCore()
{
	if(h_LanguageDLL)
		FreeLibrary(h_LanguageDLL);
}

MultiLanguage::MultiLanguageCore* MultiLanguage::MultiLanguageCore::GetCore()
{

	if(Core.h_LanguageDLL != NULL)
		return &Core;

	HMODULE hmodule = LoadLibrary(_T("Multilanguage.dll"));
	if(hmodule == NULL)
	{	
		return NULL;
	}
	Core.procSwitchLanguage = (Proc_SwitchLanguage) GetProcAddress(hmodule, "SwitchLanguage");
	Core.procGetStringByStr = (Proc_GetStringByStr) GetProcAddress(hmodule, "GetStrByStr");
	Core.procGetCurLanguage = (Proc_GetCurLanguage) GetProcAddress(hmodule, "GetCurLanguage");
	
	return &Core;
}


BOOL	MultiLanguage::SwitchLanguage(const LANGID langID)
{
	if(pCore == NULL || pCore->procSwitchLanguage == NULL)
		return FALSE;

	return pCore->procSwitchLanguage(langID) == true;
}


UINT	MultiLanguage::GetStringByStr(LPCTSTR keyStr, LPTSTR buf, UINT bufSize)
{
	if(!(pCore == NULL || pCore->procGetStringByStr == NULL))
	{
		UINT size = pCore->procGetStringByStr(keyStr, buf, bufSize);
		if(size != 0)
		{
			return size;
		}
	}
	_tcsncpy(buf, keyStr, bufSize);
	return (UINT) _tcslen(keyStr);
}

BOOL	MultiLanguage::GetStringByStrEx(LPCTSTR keyStr, LPTSTR buf, const UINT bufSize, UINT* buflength)
{
	size_t size;
	if(!(pCore == NULL || pCore->procGetStringByStr == NULL))
	{
		size = pCore->procGetStringByStr(keyStr, buf, bufSize);
		if(size !=0)
		{
			if(buflength != NULL)
				*buflength = (UINT) size;
			return TRUE;
		}
	}
	_tcsncpy(buf, keyStr, bufSize);
	size = _tcslen(keyStr);
	if(buflength != NULL)
		*buflength = (UINT) size;
	return FALSE;
}

LPCTSTR	MultiLanguage::GetStringByStr(LPCTSTR keyStr)
{
	if(m_innerbuflength == 0)
	{
		m_innerbuflength = 1034;
		m_pinnerbuf = new TCHAR[m_innerbuflength];
	}
	
	memset(m_pinnerbuf, 0, sizeof(TCHAR)*m_innerbuflength);
	int ret = (int) GetStringByStr(keyStr, m_pinnerbuf, m_innerbuflength - 10);
	if(ret >= (m_innerbuflength - 10))
	{
		m_innerbuflength = ret + 10;
		delete[] m_pinnerbuf;
		m_pinnerbuf = new TCHAR[m_innerbuflength];
		GetStringByStr(keyStr, m_pinnerbuf, m_innerbuflength);
	}
	return m_pinnerbuf;
}

LANGID	MultiLanguage::GetCurLanguage()
{
	if(!(pCore == NULL || pCore->procGetCurLanguage == NULL))
	{
		LANGID id;
		if(pCore->procGetCurLanguage(id))
			return id;
		else
			return 0x0409;	//default langid
	}
	return 0x0409;
}